package com.py.androidUtils.UI.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.py.androidUtils.UI.model.PositionView;
import com.py.androidUtils.utils.LogUtils;

/**
 * @说明 定位工具
 * @作者 LY
 * @文件 LocationUtils.java
 * @时间 2015年7月18日 下午6:35:25
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public class LocationUtils {
	private LocationClient mLocationClient;
	private MyLocationListener mMyLocationListener;
	private PoiSearch mPoiSearch = null;
	private LoadLocation loadLocation;
	private String cityName = "";
	private String addressName = "";
	private int page = 0;
	private Context mContext;

	public LocationUtils(Context context) {
		this.mContext = context;
	}

	public void initLocation() {
		mLocationClient = new LocationClient(mContext);
		mMyLocationListener = new MyLocationListener();
		LocationClientOption option = new LocationClientOption();
		mLocationClient.registerLocationListener(mMyLocationListener);
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
		mLocationClient.start();
	}

	public void initPoi() {
		mPoiSearch = PoiSearch.newInstance();
		LogUtils.i("======>>>>" + page);
		mPoiSearch.searchInCity((new PoiCitySearchOption()).city(cityName).keyword(addressName).pageNum(page));
		mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
			@Override
			public void onGetPoiResult(PoiResult result) {
				if (result == null) {
					return;
				}
				List<PositionView> list = new ArrayList<PositionView>();
				if (result.error == SearchResult.ERRORNO.NO_ERROR) {
					page++;
					for (int i = 0; i < result.getAllPoi().size(); i++) {
						PositionView positionView = new PositionView();
						PoiInfo poiInfo = result.getAllPoi().get(i);
						positionView.setTitle(poiInfo.name);
						positionView.setContent(poiInfo.address);
						list.add(positionView);
					}
				}
				loadLocation.getPosition(list);
			}

			@Override
			public void onGetPoiDetailResult(PoiDetailResult arg0) {
			}
		});
	}

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			cityName = location.getCity();
			addressName = location.getStreet();
			LogUtils.i(addressName);
			if ((cityName != null && cityName.trim().length() > 0) && (addressName != null && addressName.trim().length() > 0)) {
				initPoi();
			}
		}
	}

	public interface LoadLocation {
		void getPosition(List<PositionView> list);
	}

	public void setLoadLocation(LoadLocation loadLocation) {
		this.loadLocation = loadLocation;
	}
}
